﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace OneSchema
{
    public class RelationNode : INotifyPropertyChanged
    {
        private IList<RelationNode> m_Children;
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Create a relation node with a relation item
        /// </summary>
        public RelationNode(Relation item)
        {
            ObservableCollection<RelationNode> childrenCollection = new ObservableCollection<RelationNode>();
            childrenCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(childrenCollection_CollectionChanged);
            Children = childrenCollection;

            this.NodeItem = item;
        }

        #region Public Properties

        public Relation NodeItem { get; set; }
        public IList<RelationNode> Children 
        { 
            get { return m_Children; }
            set
            {
                m_Children = value;
                OnPropertyChanged("Children");
            }
        }

        #endregion

        void childrenCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged("Children");
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
